export default class AjaxResult{
    readonly success: boolean;
    readonly code: number;
    readonly message: string;
    [key: string]: any;

    constructor(success: boolean, code: number, message: string, data?: any) {
        this.success = success;
        this.code = code;
        this.message = message;
        Object.assign(this, data);
    }

    static success(data?: any, msg = "操作成功"): AjaxResult {
        let newData: any = null;
        // 如果返回值是基本类型或者数组，就组装一下。
        if (typeof data !== 'object' || data instanceof Array) {
            newData = { data };
        } else {
            newData = data;
        }
        return new AjaxResult(true, 200, msg, newData);
    }

    static error(code = 500, msg = "操作失败", data?: any): AjaxResult {
        return new AjaxResult(false, code, msg, data);
    }
}